<?php
/**
 * 控制器抽象类
 *
 */
abstract class Controller
{
    /**
     * 模板类
     *
     * @var obj;
     */
    var $tpl;
    var $in_var;
    var $lang;
    var $sys_conf_var;


    /**
     * 构造函数
     *
     */
    function __construct()
    {

        global $tpl,$in_var,$admin_lang,$sys_info,$sys_conf_var;

        $this->tpl            = $tpl;
        $this->in_var         = $in_var;
        $this->lang           = $admin_lang;
        $this->sys_info       = $sys_info;
        $this->sys_conf_var   = $sys_conf_var;
    }

    /**
     * 加载模型
     *
     * @param string $class
     */
    function loadModel($class)
    {
        $numargs = func_num_args();

        if($numargs == 1)
        {
            $this->$class = new $class();
        }
        else
        {
            $arg_list = func_get_args();

            for ($i = 0; $i < $numargs; $i++) {
                if($i > 0)
                {
                    $args[] = '$arg_list[' . $i . ']';
                }
            }

            eval('$this->$class = new $class(' . implode(',',$args) . ');');
        }
    }

    /**
     * 加载类库文件
     *
     * @param string $class
     */
    function loadClass($class)
    {
        $numargs = func_num_args();

        if($numargs == 1)
        {
            $this->$class = new $class();
        }
        else
        {
            $arg_list = func_get_args();

            for ($i = 0; $i < $numargs; $i++) {
                if($i > 0)
                {
                    $args[] = '$arg_list[' . $i . ']';
                }
            }

            eval('$this->$class = new $class(' . implode(',',$args) . ');');
        }
    }

    /**
     * destruct function
     *
     */
    function __destruct()
    {
        //debug();
    }

    /**
     * 公用初始化函数：检查登录状态，加载公用 Model，等等
     * @param $shall_return     是否返回检查结果，默认：否，直接跳转或输出 json（针对 ajax 请求）
     * @return Array|boolean
     */
    protected function initSession($shall_return=false)
    {
        $this->loadModel('user_model');
        $this->loadModel('auth_model');

        $is_ajax = $this->in_var['ajax'];

        // 获得当前登录的用户信息
        $session_info = $this->auth_model->getSessionInfo();

        if (!$session_info)
        {
            $result = array(
                'error' => 'login',
            );
            if ($shall_return === true)
            {
                return $result;
            }
            if ($is_ajax)
            {
                echo json_encode($result);
            }
            else
            {
                // jump to login page
                goto_url(@get_url('user', 'login'), 'header', false, true);
            }
        }

        $this->my_uid   = $session_info['uid'];
        $this->my_info  = $this->user_model->getUserInfo($this->my_uid);

        if (!$this->my_info)
        {
            $result = array(
                'error' => 'register',
            );
            if ($shall_return === true)
            {
                return $result;
            }
            if ($is_ajax)
            {
                echo json_encode($result);
            }
            else
            {
                // jump to register page
                goto_url(@get_url('user', 'register'), 'header', false, false);
            }
        }

        if (!$is_ajax)
        {
            $this->loadModel('app_model');
            $this->app_model->syncApp($this->my_uid);

            $app_list = $this->app_model->userGetAppList($this->my_uid);
            $this->tpl->assign('app_list', $app_list);
        }

        $this->tpl->assign('my_uid',   $this->my_uid);
        $this->tpl->assign('my_info',  $this->my_info);

        return true;
    }
    
    /**
     * 公用检查是否加入班级函数：检查班级状态，加载公用 Model，等等
     * @param $shall_return     
     * @return Array|boolean
     */
    protected function classes($app)
    {
    	$this->initSession();
    	
    	$user_info = $this->my_info;
    	
    	$this->loadModel('app_model');
    	$app_info = $this->app_model->getAppInfo($app);

    	if ( !$user_info['class_id'] )//判断是否有班级
        {
        	$this->loadModel('class_model');
        	$this->loadModel('school_model');
        	$this->loadModel('user_statistics_model');
        	$key     = 'is_send';
       		$msg     = unserialize($this->user_statistics_model->getStat($this->my_uid, $key));

       		// 读取班级信息
        	$class_info = $this->class_model->getClassInfo($msg['class_id']);
        	$school_info= $this->school_model->getSchoolInfo($class_info['school_id']);
        	
        	if ( $msg['flag'] === 0 )
        	{
        		$this->tpl->template_dir = SYS_PATH  . 'template/comm/';
        		
        		$this->tpl->assign('class_info', $class_info);
        		$this->tpl->assign('school_info', $school_info);
        		$this->tpl->assign('app_info', $app_info);
        		
        		$this->tpl->display('joinclasssuccess.html');
        		exit;
        	}
        	else
        	{
	           	$this->tpl->template_dir = SYS_PATH  . 'template/comm/';
	
	           	$this->tpl->assign('app_info', $app_info);
	        	$this->tpl->display('noclass.html');
	        	exit;
        	}
        }
    }
}
